package touchtrack.view.scene;

import java.awt.Color;
import java.awt.event.KeyEvent;

import org.mt4j.MTApplication;
import org.mt4j.components.visibleComponents.font.FontManager;
import org.mt4j.components.visibleComponents.font.IFont;
import org.mt4j.components.visibleComponents.widgets.MTImage;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.input.gestureAction.DefaultLassoAction;
import org.mt4j.input.gestureAction.DefaultPanAction;
import org.mt4j.input.gestureAction.DefaultRotateAction;
import org.mt4j.input.inputProcessors.componentProcessors.lassoProcessor.LassoProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.panProcessor.PanProcessorTwoFingers;
import org.mt4j.input.inputProcessors.componentProcessors.rotateProcessor.RotateProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.zoomProcessor.ZoomProcessor;
import org.mt4j.sceneManagement.AbstractScene;
import org.mt4j.util.MTColor;
import org.mt4j.util.camera.MTCamera;
import org.mt4j.util.math.Vector3D;

import processing.core.PImage;
import touchtrack.Settings;
import touchtrack.view.gesture.TrackDetailGestureListener;
import touchtrack.view.graph.TrackNode;

/**
 * TrackDetailScene
 * 
 * Multitouch scene class.
 * 
 * @author	MuMe09 team 6
 *         	http://mumetech.wordpress.com/
 */
public class TrackDetailScene extends AbstractScene
{
	private MTApplication app;
	private TrackGraphScene graphScene;
	private IFont font_header;
	private IFont font_default;
	private Thread playThread;
        
    /**
     * Constructor.
     * 
     * @param  	MTApplication mtApplication
     * @param	TrackGraphScene graphScene
     * @param	TrackNode track
     */
    public TrackDetailScene(MTApplication mtApplication, TrackGraphScene graphScene, TrackNode track) 
    {
    	super(mtApplication, "TouchTrack");
    	this.app = mtApplication;
    	this.graphScene = graphScene;
    	
    	// Create fonts
		this.font_header = FontManager.getInstance().createFont(
			app, 
			"arial.ttf", 
			25,
			new MTColor(255, 255, 255, 255),   	//Font fill color
			new MTColor(255, 255, 255, 255)); 	//Font outline color;
		
		this.font_default = FontManager.getInstance().createFont(
				app, 
				"arial.ttf", 
				12,
				new MTColor(255, 255, 255, 255),   	//Font fill color
				new MTColor(255, 255, 255, 255)); 	//Font outline color;
    	
    	// Set track
    	this.track = track;
    	
    	// If LocalTrack => play
    	final TrackNode playTrack = track;
    	playThread = new Thread()
    	{
            public void run()
            {
            	playTrack.getTrackInterface().play();
            }
    	};
    	//playThread.start();
    	
        // Set the background color
        app.setBackground(new Color(0));
        	
		// Set a zoom limit
        final MTCamera camManager = new MTCamera(app);
        this.setSceneCam(camManager);
        this.getSceneCam().setZoomMinDistance(80);
        
        // Disable frustum culling for this scene - optional
        this.getCanvas().setFrustumCulling(false);
        
        // Lasso listener (???)
        //LassoProcessor lassoProcessor  = new LassoProcessor(app, this.getCanvas(), this.getSceneCam());
        //this.getCanvas().registerInputProcessor(lassoProcessor);
        //this.getCanvas().addGestureListener(LassoProcessor.class, new DefaultLassoAction(app, this.getCanvas().getClusterManager(), this.getCanvas()));
        
        // Pan listener
        this.getCanvas().registerInputProcessor(new PanProcessorTwoFingers(app));
        this.getCanvas().addGestureListener(PanProcessorTwoFingers.class, new DefaultPanAction());

        // Zoom listener
        this.getCanvas().registerInputProcessor(new ZoomProcessor(app));
        this.getCanvas().addGestureListener(ZoomProcessor.class, new TrackDetailGestureListener(app, graphScene));
        
        // Rotate listener
        //this.getCanvas().registerInputProcessor(new RotateProcessor(app));
        //this.getCanvas().addGestureListener(RotateProcessor.class, new DefaultRotateAction());
        
        // Show track image
		PImage image = Settings.toPImage(track.getTrackInterface().getImage());
		MTImage imageArea = new MTImage(image, app);
		imageArea.setPositionGlobal(new Vector3D(app.width/2f, app.height/4f));
		imageArea.setSizeLocal(track.getWidth(), track.getHeight());
		imageArea.setNoFill(true);
		imageArea.setNoStroke(true);
		this.getCanvas().addChild(imageArea);
		
        // Show track title
        MTTextArea title = new MTTextArea(app, font_header);
        title.setNoFill(true);
        title.setNoStroke(true);
        title.setText(track.toString());
        title.setPositionGlobal(new Vector3D(app.width/2f, app.height/2f));
        this.getCanvas().addChild(title);
        
        // Show track summary
        MTTextArea summary = new MTTextArea(app, font_default);
        summary.setNoFill(true);
        summary.setNoStroke(true);
        summary.setText(track.getTrackInterface().getSummary());
        //summary.setWidthLocal(300);
        if(track.getTrackInterface().getSummary()!=null)
        {
        	String ts = track.getTrackInterface().getSummary().replace(".  ", ".\n");
        	ts = ts.replace(". ", ".\n");
        	ts = ts.replace("&quot;","\"");
        	ts = ts.replace(", ", ",\n");
        	summary.setText(ts);
        }
        summary.setPositionGlobal(new Vector3D(app.width/2f, app.height/1.5f));
        this.getCanvas().addChild(summary);
    }
    
    /**
     * @param	e
     */
    public void keyEvent(KeyEvent e)
    {
        int evtID = e.getID();
        if (evtID != KeyEvent.KEY_PRESSED)
            return;

        switch (e.getKeyCode()){
        case KeyEvent.VK_M:
//                      Iscene[] scenes = mtApp.getScenes();
//                      Iscene modScene = null;
//                      for (int i = 0; i < scenes.length; i++) {
//                              Iscene iscene = scenes[i];
//                              if (iscene instanceof MainModerationScene){
//                                      modScene = iscene;
//                              }
//                      }
//                      if (modScene != null){
//                              mtApp.changeScene(modScene);
//                              mtApp.removeScene(this);
//                      }
            break;
        case KeyEvent.VK_BACK_SPACE:
            app.popScene();
            break;
            default:
                break;
        }
    }
    
    @Override
    public void init()
    {
        app.registerKeyEvent(this);
    }

    @Override
    public void shutDown()
    {
        app.unregisterKeyEvent(this);
    }
    
	/***************************************************************************
	 * 								Track
	 ***************************************************************************/
    
	private TrackNode track;

    /***************************************************************************
     *                             Java stuff
     ***************************************************************************/
    
    private static final long serialVersionUID = 1L;
}